#pragma once

// 大致功能要有个初步了解
// 1.成员变量
// 2.核心成员函数
// 3.根据名字连蒙带猜

//template<class T>
//class vector
//{
//public:
//	typedef T* iterator;
//private:
//	T* _a;
//	size_t _size;
//	size_t _capacity;
//};

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <assert.h>

using namespace std;

namespace HQ
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last) {
				push_back(*first);
				++first;
			}
		}

		// 强制生成默认的
		vector() = default;

		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (const auto& e : v) {
				push_back(e);
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		// 赋值拷贝构造
		// 直接传参先构造
		// v1 = v2;
		vector<T>& operator=(vector<T> v)
		{
			this->swap(v);
			return *this;
		}

		// 注意这个匿名对象
		vector(size_t n, const T& x = T())
		{
			reserve(n);
			for (size_t i = 0; i < n; i++) {
				push_back(x);
			}
		}

		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (const auto& e : il){
				push_back(e);
			}
		}

		~vector()
		{
			if (_start) {
				delete[] _start;
				_start = _finish = _end_of_storage = nullptr;
			}
		}

		void reserve(size_t n)
		{
			if (n > capacity()) {
				T* tmp = new T[n];
				size_t oldsize = _finish - _start; // 提前存储

				if (_start != nullptr) {
					//// 对任意类型都是浅拷贝
					//memcpy(tmp, _start, sizeof(T) * size()); 
					for (size_t i = 0; i < oldsize; i++) {
						tmp[i] = _start[i];
					}

					delete[] _start;
				}

				_start = tmp;
				_finish = _start + oldsize; // 不能用size()，_start已经被更新
				_end_of_storage = _start + n;
			}

		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		T& operator[](size_t i)
		{
			assert(i < size());
			return _start[i];
		}

		const T& operator[](size_t i) const
		{
			assert(i < size());
			return _start[i];
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage) {
				// 满了
				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);
			}

			*_finish = x;
			++_finish;
		
		}

		void pop_back()
		{
			assert(_start != _finish); // 不为空
			--_finish;
		}

		// 通过返回值来访问
		// 这是因为pos作为形参不会影响实参，扩容的时候可能会是定时炸弹
		// 因此这里返回更新后的pos值，否则迭代器就失效了
		iterator insert(iterator pos, const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			if (_finish == _end_of_storage) {
				// 满了，pos新开的空间要换一下
				size_t len = pos - _start; // 记录长度

				size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
				reserve(newcapacity);

				pos = len + _start; // 更新pos位置
			}

			iterator end = _finish;
			while (end > pos) {
				*end = *(end - 1);
				--end;
			}
			*pos = x;
			++_finish;

			return pos;
		}

		void erase(iterator pos)
		{
			assert(pos >= _start);
			assert(pos < _finish);

			iterator it = pos + 1;
			while (it != _finish) {
				*(it - 1) = *it;
				++it;
			}

			--_finish;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};

	void test_vector1()
	{
		vector<int> v1;

		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		
		for (size_t i = 0; i < v1.size(); i++) {
			cout << v1[i] << " ";
		}
		cout << endl;
	}

	void test_vector2()
	{
		vector<int> v1;

		v1.push_back(1);
		v1.push_back(3);
		v1.push_back(2);
		v1.push_back(4);

		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;
	}

	void test_vector3()
	{
		vector<int> v1;

		v1.push_back(1);
		v1.push_back(3);
		v1.push_back(2);
		v1.push_back(4);

		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;

		v1.insert(v1.begin(), 0); // 0 1 3 2 4
		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;

		v1.insert(v1.end(), 5); // 0 1 3 2 4 5
		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;

		v1.insert(v1.begin() + 2, 8); // 0 1 8 3 2 4 5
		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;
	}

	void test_vector4()
	{
		vector<int> v1;

		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		vector<int>::iterator it = find(v1.begin(), v1.end(), 3);
		if (it != v1.end()) {
			/*v1.insert(it, 114);*/
			it = v1.insert(it, 114);
			cout << *it << endl;
		}

		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;
	}

	void test_vector5()
	{
		vector<int> v1;

		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);

		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;

		v1.erase(v1.begin());
		for (const auto& num : v1) {
			cout << num << " ";
		}
		cout << endl;
	}

	void test_vector6()
	{
		std::vector<int> v1;

		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(5);

		int x;
		cin >> x;

		std::vector<int>::iterator it = find(v1.begin(), v1.end(), x);
		if (it != v1.end()) {
			it = v1.erase(it); // correct
			//v1.erase(it);
			
			// it被界定为失效了
			// 一是考虑部分平台缩容
			// 二是假如是尾删，那么这个时候it就确确实实是野指针了
			// 因为有这种可能，所以我们认为失效
			if (it != v1.end()) {
				cout << *it << endl; // err，不允许访问
			}
		}
	}

	void test_vector7()
	{
		// 兼容模板
		int i = 0;
		int j(1);
		int k = int();
		int x = int(2);
		std::vector<int> v = std::vector<int>();

		cout << i << " ";
		cout << j << " ";
		cout << k << " ";
		cout << x << " ";
		cout << endl;

		vector<string> v1(10);
		vector<string> v2(10, "xxx");

		for (const auto& str : v2) {
			cout << str << endl;
		}
	}

	void test_vector8()
	{
		vector<string> v1;

		v1.push_back("11111111");
		v1.push_back("11111111");
		v1.push_back("11111111");
		v1.push_back("11111111");
		v1.push_back("11111111");
		
		for (const auto& e : v1) {
			cout << e << endl;
		}
		cout << endl;

 	}

	void test_vector9()
	{
		std::vector<int> v1(5, 0);
		std::vector<int> v2(5, 0);

		v1.resize(4);
		v2.resize(6, 1);

		for (const auto& e : v1) {
			cout << e << " ";
		}
		cout << endl;

		for (const auto& e : v2) {
			cout << e << " ";
		}
		cout << endl;
	}

	void test_vector10()
	{
		std::vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);

		v.insert(v.begin(), 0); // 在容器开头插入0
		v.insert(v.begin(), 5, -1); // 在容器开头插入5个-1
		v.erase(v.begin()); // 删除容器中的第一个元素
		v.erase(v.begin(), v.begin() + 5); // 删除在该迭代器区间内的元素（左闭右开）

	}

	void test_vector11()
	{
		std::vector<int> v;

		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);

		cout << v.front() << endl; // 1
		cout << v.back() << endl; // 4
	}

	template<class T>
	void print_vector(const vector<T>& v)
	{
		// 规定，没有实例化的类模板里面取东西，编译器不能区分这里const_iterator
		// 是类型还是静态成员变量
		//typename vector<T>::const_iterator it = v.begin();
		auto it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_vector12()
	{
		vector<int> v;

		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);

		print_vector(v);
	}
}